<script setup>
import { ref } from 'vue';
import axios from 'axios';

// 创建响应式数据
const username = ref('');
const password = ref('');
const repassword = ref('');
const phone = ref('');
const state = ref(false);

// 注册按钮点击事件
const register = async () => {
  // 简单的表单验证，确保两次输入的密码一致
  if (password.value !== repassword.value) {
    alert("两次输入的密码不一致！");
    return;
  }

  try {
    // 构建传递给后端的 JSON 参数
    const payload = {
      username: username.value,
      password: password.value,
      image: '',
      phone: phone.value,
      state: state.value
    };

    // 发起 POST 请求并传递 JSON 数据
    const response = await axios.post('http://localhost:8080/user/save', payload);

    // 根据后端返回的状态处理
    if (response.data.code === 1) {
      alert('注册成功！');
      // 在此处可以执行跳转或其他操作
    } else {
      alert('注册失败: ' + response.data.msg);
    }
  } catch (error) {
    console.error('注册请求出错', error);
    alert('注册失败，请稍后再试。');
  }
};
</script>

<template>
  <div class="container">
    <div class="registration-form">
      <h2>账号注册</h2>
      <div class="form-group">
        <label>用户名:</label>
        <input v-model="username" type="text" id="username" name="username" placeholder="请输入用户名">
        <span class="hint">*6~18个字符，可使用字母、数字、下划线</span>
      </div>
      <div class="form-group">
        <label>密码:</label>
        <input v-model="password" type="password" id="password" name="password" placeholder="请输入密码">
        <span class="hint">*6~16个字符，区分大小写</span>
      </div>
      <div class="form-group">
        <label>确认密码:</label>
        <input v-model="repassword" type="password" id="repassword" name="repassword" placeholder="请再次输入密码">
        <span class="hint">*请再次填写密码</span>
      </div>
      <div class="form-group">
        <label>手机:</label>
        <input v-model="phone" type="tel" id="telephone" name="telephone" placeholder="请输入手机号">
        <span class="hint">*忘记密码时，可以通过该手机号码快速找回密码</span>
      </div>
      <div class="form-group verification">
        <input type="text" id="message" name="message" placeholder="请输入验证码">
        <button id="getnum">获取</button>
      </div>
      <div class="agreement">
        <input type="checkbox" v-model="state" id="check" checked>
        <span>同意并遵守<a>用户使用协议</a></span>
      </div>
      <button class="register_button" @click="register">立即注册</button>
    </div>
  </div>
</template>

<style scoped>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: "HarmonyOS Sans SC", sans-serif;
}

body {
  background-color: #f5f5f5;
}

.container {
  font-family: "HarmonyOS Sans SC", sans-serif;
  width: 1500px;
  background-color: #121620;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-image: url('@/assets/login/bg.png');
  background-size: cover;
}




.header img {
  width: 100px;
}

.registration-form {
  background-color: #fff;
  padding: 40px;
  border-radius: 8px;
  box-shadow: 0 8px 16px rgba(0, 0, 0, 0.5);
  width: 80%;
  max-width: 600px;
  margin: 0 auto;
}

h2 {
  text-align: center;
  color: #333;
  margin-bottom: 30px;
  font-size: 28px;
  font-weight: 600;
}

.form-group {
  margin-bottom: 20px;
}

label {
  display: block;
  color: #333;
  font-size: 16px;
  margin-bottom: 8px;
}

input[type="text"],
input[type="password"],
input[type="email"],
input[type="tel"] {
  width: 100%;
  padding: 12px;
  border: 1px solid #ccc;
  border-radius: 4px;
  font-size: 16px;
  outline: none;
  transition: border-color 0.3s;
}

input[type="text"]:focus,
input[type="password"]:focus,
input[type="email"]:focus,
input[type="tel"]:focus {
  border-color: #509ae4;
}

.hint {
  display: block;
  font-size: 12px;
  color: #999;
  margin-top: 5px;
}

.verification {
  display: flex;
  align-items: center;
  gap: 10px;
}

button#getnum {
  padding: 10px 20px; /* 调整 padding */
  background-color: #509ae4;
  color: #fff;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  min-width: 90px; /* 设置最小宽度，防止文字换行 */
  white-space: nowrap; /* 禁止换行 */
  text-align: center; /* 确保文字在按钮中居中对齐 */
}

button#getnum:hover {
  background-color: #407fbf;
}

.agreement {
  margin-top: 20px;
  font-size: 14px;
  color: #333;
}

.agreement a {
  color: #509ae4;
  text-decoration: none;
}

.agreement a:hover {
  text-decoration: underline;
}

.register_button {
  display: block;
  width: 100%;
  padding: 12px;
  background-color: #e37200;
  color: #fff;
  border: none;
  border-radius: 4px;
  font-size: 18px;
  cursor: pointer;
  margin-top: 20px;
}

.register_button:hover {
  background-color: #d85b00;
}
</style>